package com.example.iread;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.iread.activity.LoginActivity;
import com.example.iread.application.MyApplication;
import com.example.iread.bean.UserBean;
import com.example.iread.dao.UserDao;
import com.example.iread.fragment.HomeFragment;
import com.example.iread.fragment.PersonFragment;
import com.example.iread.http.UserApi;
import com.example.iread.util.WindowUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = "SoRiya";
    int[] icon = {R.mipmap.ic_tabbar_home, R.mipmap.ic_tabbar_person};
    int[] iconActive = {R.mipmap.ic_tabbar_home_active, R.mipmap.ic_tabbar_person_active};
    Fragment[] page = {new HomeFragment(), new PersonFragment()};
    private ViewGroup tabbar_linear_layout;
    private FragmentManager fragmentManager;
    private View tabbar_home, tabbar_person;
    private int currentPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "onCreate: Home");

        Intent intent = getIntent();
        currentPage = intent.getIntExtra("page", 0);

//        WindowUtil.setTransparentStatusBar(getWindow());

        // 获取帧布局管理器
        fragmentManager = getSupportFragmentManager();

        // 判断是否登录
        isLogin();

        if (savedInstanceState != null && tabbar_person != null) {
            // 切换深色模式导致Activity重新加载
            tabbar_person.callOnClick();
        }
    }

    private void initFragment() {
        tabbar_linear_layout = findViewById(R.id.tabbar_linear_layout);
        tabbar_home = findViewById(R.id.tabbar_home);
        tabbar_person = findViewById(R.id.tabbar_person);

        tabbar_home.setOnClickListener(this);
        tabbar_person.setOnClickListener(this);

        if (currentPage == 0) tabbar_home.callOnClick(); // 默认点击第一个Tabbar按钮
        else tabbar_person.callOnClick();
    }

    @Override
    public void onClick(View view) {
        int index = 0; // tabbar下标
        for (int i = 0; i < tabbar_linear_layout.getChildCount(); i++) {
            View childAt = tabbar_linear_layout.getChildAt(i);

            // 排他
            // 先全部设为未选择状态
            ImageView imageView = (ImageView) ((ViewGroup) childAt).getChildAt(0);
            imageView.setImageResource(icon[i]);

            TextView textView = (TextView) ((ViewGroup) childAt).getChildAt(1);
            textView.setTextColor(getResources().getColor(R.color.secondary, getTheme()));

            if (childAt.getId() == view.getId()) {
                // 获取TabbarIndex
                index = i;
            }
        }

        // 设置index状态
        ImageView imageView = (ImageView) ((ViewGroup) view).getChildAt(0);
        imageView.setImageResource(iconActive[index]);
        TextView textView = (TextView) ((ViewGroup) view).getChildAt(1);
        textView.setTextColor(getResources().getColor(R.color.primary, getTheme()));

        // 切换视图
        // 获取帧事务，开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fl, page[index]); // 替换页面
        fragmentTransaction.commit(); // 提交事务
    }

    private void isLogin() {
        SharedPreferences userInfo = getSharedPreferences("userInfo", MODE_PRIVATE);
        String token = userInfo.getString("token", null);

        UserDao userDao = MyApplication.getInstance().getDb().userDao();

        if (token != null && userDao.getOne() != null) {
            UserApi.getUserInfo(res -> {
                if (res.code == 0) {
                    // 未登录
                    userDao.clear();
                    SharedPreferences.Editor edit = userInfo.edit();
                    edit.remove("token");
                    edit.apply();
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    // 已登录，加载Fragment
                    new Handler(Looper.getMainLooper()).post(this::initFragment);
                }
            });
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
    }
}